/* wpa_ctrl.h */
#ifndef WPA_CTRL_H
#define WPA_CTRL_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <sys/un.h>
#include<sys/time.h>
#include <pthread.h>
#define CTRL_CLIENT_DIR "/var/run/wpa_supplicant/clients"
#define CTRL_CLIENT_PREFIX "wpa_ctrl_"
#define CTRL_TIMEOUT 3

typedef struct wpa_conn {
    int sockfd;
    char client_path[108];  // SUN_PATH长度
    struct sockaddr_un dest;
    bool non_block;
    pthread_t event_thread;
    void (*event_callback)(const char *msg);
    pthread_mutex_t lock;      // 新增互斥锁
    volatile bool running;     // 线程运行标志
    pthread_cond_t cond;       // 条件变量
} wpa_conn;

// 错误码
enum WPA_ERRORS {
    WPA_OK = 0,
    WPA_SOCKET_ERROR,
    WPA_BIND_ERROR,
    WPA_CONNECT_ERROR,
    WPA_SEND_ERROR,
    WPA_RECV_ERROR
};

// API接口
wpa_conn *wpa_connect(const char *ctrl_path, bool non_block, int *err);
int wpa_send_command(wpa_conn *conn, const char *cmd, char *reply, size_t *reply_len);
int wpa_recv_event(wpa_conn *conn, char *buffer, size_t buf_len);
void wpa_disconnect(wpa_conn *conn);
const char *wpa_strerror(int errcode);

#endif